package com.polaris.live.viewmodel.message

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.homeService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.go_resp.OtherUserIdBean
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

/**
 * MainMessageViewModel
 *
 * @author Created by 半仙 on 2023/10/31/031 9:49
 */
class MainMessageViewModel : BaseViewModel() {

    //消息顶部信息
    val singleLiveData = MutableLiveData<ResultState<List<DisplayUserBean>>>()

    fun singleTop() {
        request({ homeService.singleTop() }, singleLiveData)
    }

    /**
     * 关注
     */
    val followerLiveData = MutableLiveData<ResultState<Any>>()

    fun addFollower(otherId: Long) {
        requestLoading({ userService.addFollower(OtherUserIdBean(otherId)) }, followerLiveData)
    }

    //打招呼定时扫描
    val greetMessageLiveData = MutableLiveData<Unit>()
    fun launchGreetMessage() {
        viewModelScope.launch {
            while (isActive) {
                delay(TimeUnit.MINUTES.toMillis(1))
                greetMessageLiveData.value = Unit
            }
        }
    }
}